package com.kirk.crawler.ui.widget;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 *  <p> UrlLabel.java </p>
 *
 * @Author      kirkliu
 * @Date        2020-08-29 22:29
 * @version     1.0.0
 * @Description  超链接label
 */
public class UrlLabel extends JLabel {
	private static final long serialVersionUID = 1L;
	private String text, url;
	private boolean isSupported;

	public UrlLabel(String text, String url) {
		this.text = text;
		this.url = url;
		try {
			this.isSupported = Desktop.isDesktopSupported()
					&& Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);
		} catch (Exception e) {
			this.isSupported = false;
		}
		setText(false);
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				setText(isSupported);
				if (isSupported)
					setCursor(new Cursor(Cursor.HAND_CURSOR));
			}

			public void mouseExited(MouseEvent e) {
				setText(false);
			}

			public void mouseClicked(MouseEvent e) {
				try {
					Desktop.getDesktop().browse(
							new java.net.URI(UrlLabel.this.url));
				} catch (Exception ex) {
				}
			}
		});
	}

	private void setText(boolean b) {
		if (!b){
			//setText("<html><font color=blue><u>" + text);
			setText(text);
		}else{
			setText("<html><font color=red><u>" + text);
		}
	}
}
